package cn.sfa.common.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailUtil { 
     protected javax.mail.Session session = null;
     
//邮件用户名
     static String mailUser = ""; //你通过哪个帐号发送邮件
     static String host = "";     //你通过哪个主机发送邮件
     static String pwd = "";   //你所要通过的帐号的密码是多少，也就是mailUser的密码
     static String sender = "" ;     //邮件从那里发送和mailUser是一个邮箱
     static String getter = "" ;       //谁接收邮件
     static String protocolKey="";//发送协议键
     static String hostKey="";		//邮件发送主机地址键
     static String authKey="";		//邮件验证键
     static String portKey="";		//邮件发送端口键
     static String protocolValue="";//发送协议值
     static String authValue="";	//邮件验证值
     static String portValue="";	//邮件发送端口值
     {
    	 readProperties("/mail.properties");
    	 //sender=mailUser;
     }
     
  // 读取properties的全部信息
     
   public static void readProperties(String filePath) {
    
   Properties props = new Properties();
    
   try {
    
	   InputStream in = SendMailUtil.class.getClassLoader().getResourceAsStream(filePath);
	    
	   props.load(in);
	    
	   Enumeration en = props.propertyNames();
	    
	   while (en.hasMoreElements()) {
	    
		   String key = (String) en.nextElement(); 
		   String value = props.getProperty(key);
		   if(key.equals("protocol"))
		   {
			   protocolKey=value;
		   }else if(key.equals("host"))
		   {
			   hostKey=value;
		   }else if(key.equals("auth"))
		   {
			   authKey=value;
		   }else if(key.equals("port"))
		   {
			   portKey=value;
		   }else if(key.equals("mailUser"))
		   {
			   mailUser=value;
		   }else if(key.equals("mailPwd"))
		   {
			   pwd=value;
		   }else if(key.equals("mail.transport.protocol"))
		   {
			   protocolValue=value;
		   }else if(key.equals("mail.smtp.host"))
		   {
			   host=value;
		   }else if(key.equals("mail.smtp.auth"))
		   {
			   authValue=value;
		   }else if(key.equals("mail.smtp.port"))
		   {
			   portValue=value;
		   }else if(key.equals("mailSender"))
		   {
			   sender=value;
		   }
	   }
   }catch(Exception e)
   {
	   e.printStackTrace();
   }
   }
   
   
     public SendMailUtil() {
         Properties props = new Properties();          //先声明一个配置文件以便存储信息
         props.put(protocolKey,protocolValue); //首先说明邮件的传输协议
         props.put(hostKey, host);            //说明发送邮件的主机地址
         props.put(authKey, authValue);          //说明发送邮件是否需要验证，表示自己的主机发送是需要验证的
         props.put(portKey, portValue);            //说明邮件发送的端口号
   
         //session认证 getInstance()
         session = javax.mail.Session.getInstance(props,new Authenticator(){
       public PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(mailUser,pwd);
       }
         });

         //这个是跟踪后台消息。打印在控制台
         session.setDebug(true);
     }
     public void send(String from,String content,String subject) {
         try { 
          this.getter = from;
          Message msg = new MimeMessage(session);
          //设置发送者
             msg.setFrom(new InternetAddress(sender)); 
             //设置接受者
             msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(getter));
             //设置发送时间
             msg.setSentDate(new Date()); 
             //设置内容的基本机制，字体等
             String htmltext=content; 
             msg.setContent(htmltext, "text/html;charset=GBK"); 
             //设置发送主题
             msg.setSubject(subject);
             //设置邮件内容
    //         msg.setText(htmltext); //如果以html的格式发送邮件那么邮件的内容需要通过setContent来设置并且不能用setText

             Transport transport = session.getTransport(protocolValue); //得到发送协议
             transport.connect(host, mailUser, pwd);//与发送者的邮箱相连
             transport.send(msg); //发送消息
             
         } catch (Exception ee) {
             ee.printStackTrace();
         }

     }

}

